
/*
 * Copyright (c) 2013.
 * 游戏服务器核心代码编写人陈磊拥有使用权
 * 联系方式：E-mail:13638363871@163.com ;qq:502959937
 * 个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver.serverCore.controller.handleController.LoginController;

import com.dc.gameserver.baseConfig.Config;
import com.dc.gameserver.proto.adminserverprotoVo.creatRoleRequest;
import com.dc.gameserver.proto.adminserverprotoVo.creatRoleResponse;
import com.dc.gameserver.proto.adminserverprotoVo.roleVo;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import javolution.util.FastTable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author : 石头哥哥
 *         Project : LandlordsServer
 *         Date: 13-6-4
 *         Time: 上午11:49
 *         Connect: 13638363871@163.com
 *         创建角色处理逻辑
 */
@Service
public class creatRoleController extends AbstractController {


    @Resource
    private PlayerService playerService;
    @Resource
    private ModelCacheService modelCacheService;

    @Override
    public void PostConstruct() {
        int ID= creatRoleRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]=creatRoleRequest.getDefaultInstance();
    }

     /**
     * messageLite数据结构体分发
     *
     * @param messageLite
     * @param player
     * @throws Exception
      *
     */
    @Override
    @SuppressWarnings("unchecked")
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        creatRoleRequest creatRoleRequest= (creatRoleRequest) messageLite;
        //check role is excite or not
        String  roleName=    creatRoleRequest.getRoleName();
        byte         sex     = (byte) creatRoleRequest.getRoleSex();
        int         roleType    =   creatRoleRequest.getRoleType();//角色类型
        creatRoleRequest=null;
        //  创建结果：1:创建角色成功 ；2.最多只能输入7个字符，21字节 ；3.角色名称重复
        creatRoleResponse.Builder builder=creatRoleResponse.newBuilder();
        if (roleName.getBytes().length>0x15){
            builder.setResult(0x2);
            player.DirectSend(wrappedBufferShort(creatRoleResponse.msgID.ID_VALUE,builder.build()));
            return;
        }
        TRole tRole=playerService.checkRole(roleName);
        if (tRole!=null){
            builder.setResult(0x3);   // 角色被注册
            player.DirectSend(wrappedBufferShort(creatRoleResponse.msgID.ID_VALUE,builder.build()));
        }else {

            //初始化角色信息，保存player到cache
            tRole  =new TRole();
            tRole.setRolename(roleName);
            tRole.setRolesex(sex); //角色性别
            tRole.setRoletype(roleType);   //角色类型
            tRole.setUserid(player.getUserID());   //角色帐号编号

            //创建 角色
            playerService.creatRole(tRole);

            player.setID(tRole.getRoleid());
            player.setName(tRole.getRolename());//角色名称

            //add role name to cache
            FastTable<String> roleNameList= (FastTable<String>) modelCacheService.findList(Config.DEFAULT_VALUE.SERVER_VALUE.gameserverPort);
            roleNameList.addLast(roleName);
            modelCacheService.addList(Config.DEFAULT_VALUE.SERVER_VALUE.gameserverPort,roleNameList);

            //角色背包系统
            player.nextItemCell();

            builder.setResult(0x1);

            /**
             *推送角色基础信息
             */
            //推送角色信息 ,包含道具  装备 秘籍  技能
            roleVo.Builder _roleVo=roleVo.newBuilder();
            _roleVo.setRoleID(tRole.getRoleid());
            _roleVo.setRoleName(tRole.getRolename());
            _roleVo.setRoleType(tRole.getRoletype());
            _roleVo.setRoleSex(tRole.getRolesex());



            _roleVo.setRoleMaxBlood(tRole.getRolemaxblood());
            _roleVo.setRoleMaxMagic(tRole.getRolemaxmagic());
            _roleVo.setRoleRecoveryMagic(tRole.getRolerecoverymagic());
            _roleVo.setRoleMaxShiled(tRole.getRolemaxshiled());
            _roleVo.setRoleAttack(tRole.getRoleattack());
            _roleVo.setRoleDefence(tRole.getRoledefence());
            _roleVo.setRoleBaseMinMomentum(tRole.getRolebaseminmomentum());
            _roleVo.setRoleBaseMaxMomentum(tRole.getRolebasemaxmomentum());
            _roleVo.setRoleMaxSual(tRole.getRolemaxsual());
            _roleVo.setRoleSualRecovery(tRole.getRolesualrecovery());
            _roleVo.setRoleExperiences(tRole.getRoleexperiences());
            _roleVo.setRoleLingshiNum(tRole.getRolelevel());
            _roleVo.setRoleMurder(tRole.getRolemurder());
            _roleVo.setRoleMurderLevel(tRole.getRolemurderlevel());
            _roleVo.setRoleLuck(tRole.getRoleluck());
            _roleVo.setRoleLingshiNum(tRole.getRolelingshinum());
            _roleVo.setRoleXianYuNum(tRole.getRolexianyunum());
            _roleVo.setRoleLevel(tRole.getRolelevel());


            _roleVo.setRoleCreatTime(tRole.getRolecreattime().getTime()); //角色创建时间
            _roleVo.setRoleSoual(tRole.getRolesual()); //角色当前神识值
            _roleVo.setRoleFiveType(tRole.getRolefivetype()); //角色五行属性
            _roleVo.setRoleSmalState(tRole.getRolesmalstate());//角色小境界    123456
            _roleVo.setRoleMaxState(tRole.getRolemaxstate());//角色大境界      1
            _roleVo.setRoleLingQi(tRole.getRolestateexperies());//境界经验

            int minCount= (int) (tRole.getRolebaseminmomentum()+tRole.getRoleexperiences()/Math.pow(0xa,tRole.getRolesmalstate()));
            int maxCount= (int) (tRole.getRolebasemaxmomentum()+tRole.getRolemurder()*(0xa-(tRole.getRolemaxstate()-1))/Math.pow(0x64,tRole.getRolemaxstate()));
            _roleVo.setRoleMinMomentum(minCount);
            _roleVo.setRoleMaxMomentum(maxCount);

            builder.addRoleList(_roleVo.build());
            player.DirectSend(wrappedBufferShort(creatRoleResponse.msgID.ID_VALUE,builder.build()));
        }
    }

}
